package com.mxw.算法之美.a13字符串;

import java.util.Objects;
import java.util.StringJoiner;

public class 反转字符串中的单词 {

    public String reverseWords(String s) {
        String[] s1 = s.split(" ");

        int l = 0;
        int r = s1.length - 1;
        while (l < r) {
            String temp = s1[l];
            s1[l] = s1[r];
            s1[r] = temp;
            l++;
            r--;
        }

        StringJoiner sj = new StringJoiner(" ");
        for (String str : s1) {
            if (Objects.equals("",str)){
                continue;
            }
            sj.add(str);
        }

        return sj.toString().trim();
    }

}
